<title>订单管理</title>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="order-order-index-search_from">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">ID</label>
                    <div class="layui-input-inline">
                        <input type="text" name="id" placeholder="请输入ID" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">订单号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="order_no" placeholder="请输入订单号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">收货人姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="full_name" placeholder="请输入收货人姓名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">电话</label>
                    <div class="layui-input-inline">
                        <input type="text" name="tel" placeholder="请输入电话" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-inline">
                        <script type="text/html" template lay-url="{{ layui.setter.apiHost }}/order/order/get_status" lay-done="layui.form.render();">
                            <select name="status">
                                <option value=""></option>
                                {{# layui.each(d.data, function(index, item){ }}
                                <option value="{{ index }}">{{ item }}</option>
                                {{# }); }}
                            </select>
                        </script>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="order-order-index-search_button">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <script type="text/html" id="order-order-index-toolbar_tmp">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="order-order-index-delivery">批量发货(电子面单)</button>
                <button class="layui-btn layui-btn-sm" lay-event="order-order-index-print_goods">批量打印发货单</button>
                <button class="layui-btn layui-btn-sm" lay-event="order-order-index-print_delivery">批量打印快递单</button>
            </div>
        </script>
        <div class="layui-card-body">
            <table id="order-order-index" lay-filter="order-order-index"></table>
            <script type="text/html" id="order-order-index-flag_tmp">
                {{# if(d.flag == 1){ }}
                <span class="layui-badge">风险</span>
                {{# } else { }}
                <span class="layui-badge layui-bg-green">正常</span>
                {{# } }}
            </script>
            <script type="text/html" id="order-order-index-action_button">
                <a class="layui-btn layui-btn-xs" lay-event="order-order-index-detail">订单详情</a>
                {{# if(d.status != 0){ }}
                <a class="layui-btn layui-btn-xs" lay-event="order-order-index-print_goods">发货单</a>
                <a class="layui-btn layui-btn-xs" lay-event="order-order-index-print_delivery">快递单</a>
                {{# } }}
                {{# if(d.status == 0){ }}
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="order-order-index-update_price">改价</a>
                {{# } }}
                {{# if(d.status == 0 || d.status == 1){ }}
                <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="order-order-index-update_address">修改地址</a>
                {{# } }}
            </script>
        </div>
    </div>
</div>
<script>
    layui.use(['common', 'table'], function () {
        var $ = layui.jquery,
            common = layui.common,
            view = layui.view,
            table = layui.table,
            form = layui.form,
            lay_id_prefix = 'order-order-index',//当前页id前缀
            api_action_url = layui.setter.apiHost + '/order/order';

        //表单搜索
        form.on('submit(' + lay_id_prefix + '-search_button)', function (data) {
            var field = data.field;
            field.page = 1;//默认第一页
            table.reload(lay_id_prefix, {
                where: field
            });
        });

        //表格操作
        table.render({
            elem: '#' + lay_id_prefix,
            url: api_action_url,
            toolbar: '#' + lay_id_prefix + '-toolbar_tmp',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', width: 80},
                {field: 'order_no', title: '订单号', minWidth: 200},
                {field: 'full_name', title: '收货人', width: 80},
                {field: 'tel', title: '电话', width: 120},
                {field: 'payment', title: '支付方式', width: 100},
                {field: 'subtotal', title: '支付金额', minWidth: 100},
                {field: 'username', title: '用户名', width: 120},
                {field: 'pay_at', title: '支付时间', width: 170},
                {field: 'created_at', title: '下单时间', width: 170},
                {field: 'status_text', title: '状态', width: 100, fixed: 'right'},
                {field: 'flag', title: '风险单', width: 80, fixed: 'right', templet: '#' + lay_id_prefix + '-flag_tmp'},
                {title: '操作', width: 260, align: 'left', fixed: 'right', toolbar: '#' + lay_id_prefix + '-action_button'}
            ]],
            page: true
        });

        //头部工具栏操作
        table.on('toolbar(' + lay_id_prefix + ')', function (obj) {
            var type = obj.event;
            data_id = get_id();
            switch (type) {
                case lay_id_prefix + '-delivery':
                    common.open_window('批量发货', api_action_url, 'order/order/batch_delivery', 'order-order-batch_delivery', {width: '100%'}, {id: data_id});
                    break;
                case lay_id_prefix + '-print_goods':
                    common.open_window('批量打印发货单', api_action_url, 'order/order/print_goods', 'order-order-print_goods', {width: '100%'}, {id: data_id});
                    break;
                case lay_id_prefix + '-print_delivery':
                    common.open_window('批量发货', api_action_url, 'order/order/print_delivery', 'order-order-print_delivery', {width: '100%'}, {id: data_id});
                    break;
            }
        });

        //监听工具条操作按钮
        table.on('tool(' + lay_id_prefix + ')', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case lay_id_prefix + '-detail':
                    common.open_window('订单详情', api_action_url, 'order/order/detail', 'order-order-detail', {width: '100%'}, {id: data.id});
                    break;
                case lay_id_prefix + '-update_price':
                    var update_price_data = {
                        id: data.id,
                        width: '500px',
                        detail_url: api_action_url + '/get_price',//获取数据地址
                        save_url: api_action_url + '/update_price',//保存地址
                    };
                    common.open_window('改价', api_action_url, 'order/order/update_price', 'order-order-update_price', update_price_data);
                    break;
                case lay_id_prefix + '-update_address':
                    var update_address_data = {
                        id: data.id,
                        width: '500px',
                        detail_url: api_action_url + '/get_address',//获取数据地址
                        save_url: api_action_url + '/update_address',//保存地址
                    };
                    common.open_window('修改地址', api_action_url, 'order/order/update_address', 'order-order-update_address', update_address_data);
                    break;
                case lay_id_prefix + '-print_goods':
                    common.open_window('打印发货单', api_action_url, 'order/order/print_goods', 'order-order-print_goods', {width: '100%'}, {id: [data.id]});
                    break;
                case lay_id_prefix + '-print_delivery':
                    common.open_window('打印快递单', api_action_url, 'order/order/print_delivery', 'order-order-print_delivery', {width: '100%'}, {id: [data.id]});
                    break;
            }
        });
        
        function get_id() {
            var data_id = new Array();
            var checkStatus = table.checkStatus(lay_id_prefix),
                select_data = checkStatus.data;
            for (var i = 0; i < select_data.length; i++) {
                data_id.push(select_data[i].id);
            }
            return data_id;
        }
    });
</script>